package com.huangmb.androidlib.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 备注:
 *
 * @author hmb
 * @version 1.0
 * @since 2016/4/4 13:53
 */
public class PreferencesUtils {
    private static final String PREFERENCE_NAME = "share_data";
    private Context mContext;
    private String mFileName;
    public PreferencesUtils(Context context){
        mContext = context;
        mFileName = PREFERENCE_NAME;
    }
    public PreferencesUtils(Context context, String fileName){
        this.mContext = context;
        this.mFileName = fileName;
    }
    public boolean put(String key, Object value) {
        SharedPreferences settings = mContext.getSharedPreferences(this.mFileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        put(editor,key,value);
        return editor.commit();
    }
    public String getString(String key) {
        return getString(key, null);
    }
    public String getString(String key, String defaultValue) {
        SharedPreferences settings = this.mContext.getSharedPreferences(this.mFileName, Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }

    public int getInt(String key) {
        return getInt(key, -1);
    }
    public int getInt(String key, int defaultValue) {
        SharedPreferences settings = mContext.getSharedPreferences(this.mFileName, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }
    public long getLong(String key) {
        return getLong(key, -1);
    }

    public long getLong(String key, long defaultValue) {
        SharedPreferences settings = mContext.getSharedPreferences(this.mFileName, Context.MODE_PRIVATE);
        return settings.getLong(key, defaultValue);
    }
    public float getFloat(String key) {
        return getFloat(key, -1);
    }

    public float getFloat(String key, float defaultValue) {
        SharedPreferences settings = mContext.getSharedPreferences(this.mFileName, Context.MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        SharedPreferences settings = mContext.getSharedPreferences(this.mFileName, Context.MODE_PRIVATE);
        return settings.getBoolean(key, defaultValue);
    }


    /**
     * put string preferences
     *
     * @param context
     * @param key The name of the preference to modify
     * @param value The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public static boolean put(Context context, String key, Object value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        put(editor,key,value);
        return editor.commit();
    }

    private static void put(SharedPreferences.Editor editor,String key,Object value){
        if (value instanceof String)
        {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean)
        {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long)
        {
            editor.putLong(key, (Long) value);
        } else
        {
            editor.putString(key, value.toString());
        }
    }
    /**
     * get string preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
     *         name that is not a string
     * @see #getString(Context, String, String)
     */
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    /**
     * get string preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a string
     */
    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }


    /**
     * get int preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     *         name that is not a int
     * @see #getInt(Context, String, int)
     */
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }

    /**
     * get int preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a int
     */
    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }


    /**
     * get long preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     *         name that is not a long
     * @see #getLong(Context, String, long)
     */
    public static long getLong(Context context, String key) {
        return getLong(context, key, -1);
    }

    /**
     * get long preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a long
     */
    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(key, defaultValue);
    }

    /**
     * get float preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
     *         name that is not a float
     * @see #getFloat(Context, String, float)
     */
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1);
    }
    /**
     * get float preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a float
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }


    /**
     * get boolean preferences, default is false
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
     *         name that is not a boolean
     * @see #getBoolean(Context, String, boolean)
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * get boolean preferences
     *
     * @param context
     * @param key The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     *         this name that is not a boolean
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getBoolean(key, defaultValue);
    }
}
